#ifndef BOARD_TIMER_H
#define BOARD_TIMER_H 1

#include "stm32f10x.h"
#include "board_NVIC.h"
#include "board_types.h"
#include "user_processing.h"

/* PWM period in microSecond. */
#define PWM_PERIOD_ANALOG          20000 //One PPM frame is 20  milliSeconds.
#define PWM_PERIOD_DIGITAL         2300  //One PPM frame is 2.3 milliSeconds.
#define PWM_DUTY_INITIAL           1000

#define PERIOD_OF_INTERUPT         1500 //2500 = 5 milliSec, 5000 = 10 milliSec
/* Be carefull.*/
/* Turn on/off PWM. */
#define TIMER1_PWM 0
#define TIMER2_PWM 0
#define TIMER3_PWM 0

#define TIMER4_PWM 0
#define TIMER4_CAPTURE 1


#if 0
/* Interrupt priority.*/
/* 0 is maximum priority, 7 minimum. */
#define TIMER1_PERIOD_INTERUPT_PRIORITY_GROUP       4
#define TIMER1_PERIOD_INTERUPT_SUB_PRIORITY_GROUP   7

#define TIMER2_CAPTURE_PRIORITY_GROUP 1
#define TIMER2_CAPTURE_SUB_PRIORITY   2

#define TIMER3_CAPTURE_PRIORITY_GROUP 1
#define TIMER3_CAPTURE_SUB_PRIORITY   3

#define TIMER4_CAPTURE_PRIORITY_GROUP 0
#define TIMER4_CAPTURE_SUB_PRIORITY   1

#define SYSTEM_TICK_PRIORITY          5

#endif

void timer_init(void);

void TIMER1_PWM_channel_init(
                               uint16_t ch_number, //1,2,3,4
                               uint16_t pwm_period, //microSec
                               uint16_t pwm_duty   //microSec
                            );
void TIMER2_PWM_channel_init(
                               uint16_t ch_number, //1,2,3,4
                               uint16_t pwm_period, //microSec
                               uint16_t pwm_duty   //microSec
                            );
void TIMER3_PWM_channel_init(
                               uint16_t ch_number, //1,2,3,4
                               uint16_t pwm_period, //microSec
                               uint16_t pwm_duty   //microSec
                            );
void TIMER4_PWM_channel_init(
                               uint16_t ch_number, //1,2,3,4
                               uint16_t pwm_period, //microSec
                               uint16_t pwm_duty   //microSec
                            );

void timer1_PWM_duty_CH1(uint16_t duty /*microSec*/);
void timer1_PWM_duty_CH2(uint16_t duty /*microSec*/);
void timer1_PWM_duty_CH3(uint16_t duty /*microSec*/);
void timer1_PWM_duty_CH4(uint16_t duty /*microSec*/);

void timer2_PWM_duty_CH1(uint16_t duty /*microSec*/);
void timer2_PWM_duty_CH2(uint16_t duty /*microSec*/);
void timer2_PWM_duty_CH3(uint16_t duty /*microSec*/);
void timer2_PWM_duty_CH4(uint16_t duty /*microSec*/);

void timer3_PWM_duty_CH1(uint16_t duty /*microSec*/);
void timer3_PWM_duty_CH2(uint16_t duty /*microSec*/);
void timer3_PWM_duty_CH3(uint16_t duty /*microSec*/);
void timer3_PWM_duty_CH4(uint16_t duty /*microSec*/);

void timer4_PWM_duty_CH1(uint16_t duty /*microSec*/);
void timer4_PWM_duty_CH2(uint16_t duty /*microSec*/);
void timer4_PWM_duty_CH3(uint16_t duty /*microSec*/);
void timer4_PWM_duty_CH4(uint16_t duty /*microSec*/);

void TIMER2_CAPTURE_channel_init(
                                  uint16_t ch_number,
                                  uint16_t TIM_TIxExternalCLKSource,
                                  uint16_t TIM_ICPolarity,
                                  uint16_t TIM_ICPSC_divider,
                                  uint16_t ICFilter
                                );
void TIMER3_CAPTURE_channel_init(
                                  uint16_t ch_number,
                                  uint16_t TIM_TIxExternalCLKSource,
                                  uint16_t TIM_ICPolarity,
                                  uint16_t TIM_ICPSC_divider,
                                  uint16_t ICFilter
                                );
void TIMER4_CAPTURE_channel_init(
                                  uint16_t ch_number,
                                  uint16_t TIM_TIxExternalCLKSource,
                                  uint16_t TIM_ICPolarity,
                                  uint16_t TIM_ICPSC_divider,
                                  uint16_t ICFilter
                                );

void TIMER1_period_interupt_init(uint16_t period_of_interupt);
void start_main_interupt_function(control_pid_est_t *board,sensor_t *gyro);
void start_main_interupt_function_sensor(sensor_raw_data_t *board,sensor_t *gyro);
void system_time_interrupt_init(void);
uint64_t read_system_time(void);
void sys_delay(uint64_t delay_time/* milliSecond*/);
void delay( unsigned int val);






#endif